<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="strategy">{{$t('添加策略')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table ref="multipleTable" size='mini' :data="whiteList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="id" label="ID" align='center' min-width="55">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" align='center' :label="$t('策略名称')" min-width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="desc" align='center' :label="$t('描述')" min-width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.desc" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="class_map" align='center' :label="$t('规则集')" min-width="500">
        <template slot-scope="scope">
          <tooltip :html="classMapShow(scope.row,';<br/>')" :content="classMapShow(scope.row,';')" class="wid190" :refName="scope.row.name"></tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ref" align='center' :label="$t('引用计数')" min-width="100"></el-table-column>
      <el-table-column align='center' width='100' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <white-dialog v-if="whiteDialogVisible" ref="refWhiteDialogVisible"></white-dialog>
  </div>
</template>
<script>
import { getWhiteList, delWhiteList } from '@/api/safe/white'
import WhiteDialog from './WhiteDialog.vue'
import ruleArray from './ruleArray'
export default {
  name: 'White',
  components: {
    WhiteDialog
  },
  data () {
    return {
      whiteDialogVisible: false,
      ruleArray: ruleArray,
      whiteList: [],
      selectList: [],
      nameStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      this.selectList = data
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getWhiteList().then(res => {
        if (res.status === 0) {
          this.whiteList = res.info
        }
      })
    },
    // 添加按钮
    dialogVisible (data) {
      this.whiteDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refWhiteDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delWhiteList({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('删除成功'))
              this.nameStr = []
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    classMapShow (data, str) {
      let reStr = ''
      data.rule_map.forEach(val => {
        if(val.proto == 'MQTT')
          val.proto = 'mqtt'
        reStr += this.$t('协议名称:') + this.$t(this.ruleArray.ruleShow[val.proto])
        switch (val.proto) {
          case 's7_300':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            var func = '0x' + '00'.substr(0, (2 - val.func.toString(16).length)) + val.func.toString(16).toUpperCase()
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.code[func]) || func)
            break
          // 协议名称:s7_300_connect,类型:关联操作,动作:允许,写寄存器区间:M区,写寄存器地址:12,写DB值:0,读寄存器区间P区;
          case 's7_300_connect':
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.opcOperateTypeList[1])
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',写寄存器区间:') + this.$t(this.ruleArray.intervalList[val.relate_write_register_interval])
            reStr += this.$t(',写寄存器地址:') + val.write_register_address_range
            reStr += this.$t(',写DB值:') + val.write_db_num
            reStr += this.$t(',读寄存器区间:') + this.$t(this.ruleArray.intervalList[val.relate_read_register_interval])
            reStr += this.$t(',读寄存器地址:') + val.read_register_address_range
            break
          case 'modbus':
            if (val.func === '0' || val.func === 0) {
              reStr += this.$t(',类型:') + this.$t(this.ruleArray.operateTypeList['1'])
              reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
              reStr += this.$t(',允许服务器返回错误的次数:') + val.error_count
            } else {
              reStr += this.$t(',类型:') + this.$t(this.ruleArray.operateTypeList['0'])
              reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
              reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.funcList[String(val.func)]) || String(val.func))
            }

            break
          case 'modbus_connect':
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.operateTypeList['2'])
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',写操作功能码:') + (this.$t(this.ruleArray.writeRegFuncList[val.write_reg_func]) || String(val.write_reg_func))
            reStr += this.$t(',读功能码:') + (this.$t(this.ruleArray.connectReadRegFuncList[val.connect_read_reg_func]) || String(val.connect_read_reg_func))
            break
          case 'iec104':
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.iesTypeList[val.type])
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            if (val.type === '0' || val.type === 0) {
              reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.iecFuncShowList[String(val.func)]) || String(val.func))
            }
            break
          // 协议名称:iec104_connect,类型:关联操作,动作:允许,写操作功能码:单点遥控,写操作传送原因:1,写操作信息对象地址:2,写操作公共地址3,关联写操作功能码单点遥控;
          case 'iec104_connect':
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.operateTypeList['0'])
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.iecFuncShowList[String(val.relate_func)]) || String(val.relate_func))
            reStr += this.$t(',写操作传送原因:') + val.relate_transfer_reason
            reStr += this.$t(',写操作信息对象地址:') + val.relate_information_object_address
            reStr += this.$t(',写操作公共地址:') + val.relate_common_address
            reStr += this.$t(',关联写操作功能码:') + (this.$t(this.ruleArray.iecFuncShowList[String(val.be_relate_func)]) || String(val.be_relate_func))
            break
          case 'OPCDA':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + this.$t(this.ruleArray.opcTypeList[val.tag_type]) || val.tag_type
            reStr += this.$t(',匹配范围:') + this.$t(this.ruleArray.acceptRangeList[val.accept_range])
            reStr += this.$t(',点名:') + val.tag_name
            if (val.tag_type === 'write' && val.accept_range === 'single') {
              reStr += this.$t(',数据类型:') + val.value_type
              reStr += this.$t(',值范围:') + val.value_range
            }
            break
          case 'OPCDA_CONNECT':
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.opcOperateTypeList[1])
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',写操作点名:') + val.be_relate_tag_name
            reStr += this.$t(',关联写操作点名:') + val.relate_tag_name
            reStr += this.$t(',关联写操作数据类型:') + val.relate_value_type
            reStr += this.$t(',关联写操作安全值范围:') + val.relate_value_range
            break
          case 'opcua':
            var valueRangeList = {
              '0': 'false',
              '1': 'true'
            }
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',消息类型:') + val.msg_type
            if (!['HEL', 'ACK', 'ERR'].includes(val.msg_type)) {
              reStr += this.$t(',服务码:') + this.ruleArray.serviceIdShowList[val.service_id]
              reStr += this.$t(',节点类型:') + this.$t(this.ruleArray.nodeIdMaskList[val.node_id_mask])
            }
            if (['999999', '673', '676', '672', '675'].includes(String(val.service_id)) && val.accept_range !== 'all') {
              reStr += ',数据值类型:' + this.ruleArray.dataMaskList[val.data_mask]
              if (val.data_mask === '1' || val.data_mask === 1) {
                reStr += ',取值范围:' + valueRangeList[(val.value_range.split('~')[0] || '0')]
              } else {
                reStr += ',取值范围:' + val.value_range
              }
            }
            break
          case 'ab':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',点名:') + val.tag_name
            reStr += this.$t(',类型:') + this.$t(this.ruleArray.tagTypeList[val.tag_type])
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteTypeList[val.read_write_type])
            if (val.tag_type === 'array_point' && val.accept_range === 'single') {
              reStr += this.$t(',起始地址:') + val.start_addr
              reStr += this.$t(',结束地址:') + val.stop_addr
            }
            break
          case 'ge':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',报文类型:') + this.$t(this.ruleArray.communicationTypeList['0x' + '0000'.substr(0, (4 - val.communication_type.toString(16).length)) + val.communication_type.toString(16).toUpperCase()])
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteTypeList[val.read_write_type])
            reStr += this.$t(',寄存器类型:') + this.$t(this.ruleArray.registerTypeList['0x' + '00'.substr(0, (2 - val.register_type.toString(16).length)) + val.register_type.toString(16).toLowerCase()])
            reStr += this.$t(',起始地址范围:') + val.start_addr + '~' + val.stop_addr
            break
          case 'dnp3':
            let dnp3FuncList = '0x' + '00'.substr(0, (2 - val.func.toString(16).length)) + val.func.toString(16).toUpperCase()
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.dnp3FuncList[dnp3FuncList]) || dnp3FuncList)
            reStr += this.$t(',对象组号:') + val.grp
            reStr += this.$t(',对象变体:') + val.var
            reStr += this.$t(',起始地址范围:') + val.start_index + '~' + val.stop_index
            break
          case 'smart200':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.ruleArray.smart200FuncList[val.func] || val.func)
            reStr += this.$t(',寄存器区间:') + this.$t(this.ruleArray.registerIntervalList[val.register_interval])
            reStr += this.$t(',DB 值:') + val.db_num
            reStr += this.$t(',起始地址范围:') + val.start_addr + '~' + val.stop_addr
            break
          case 's5':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.ruleArray.smart200FuncList[val.func] || val.func)
            reStr += this.$t(',寄存器区间:') + this.$t(this.ruleArray.s5List[val.register_interval])
            reStr += this.$t(',DB 值:') + val.db_num
            reStr += this.$t(',起始地址范围:') + val.start_addr + '~' + val.stop_addr
            break
          case 'ge_snpx':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',内存服务类型:') + this.ruleArray.memserviceTypeList[val.Memservice_type]
            reStr += this.$t(',起始地址范围:') + val.start_addr + '~' + val.stop_addr
            break
          case 'omron':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.readWriteTypeList[val.func]) || val.func)
            reStr += this.$t(',数据域:') + this.ruleArray.dataAreaList[val.data_area]
            reStr += this.$t(',起始地址范围:') + val.start_addr + '~' + val.stop_addr
            break
          case 'rtsp':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.readWriteTypeList[val.func]) || val.func)
            break
          case 'bacnet':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.bacnetFuncList[val.func]) || val.func)
            reStr += this.$t(',对象标识类型:') + val.object_type
            reStr += this.$t(',对象标识符起始地址范围:') + val.object_id_start + '~' + val.object_id_stop
            reStr += this.$t(',属性标识符起始地址范围:') + val.property_id_start + '~' + val.property_id_stop
            break
          case 'sanling':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.sanlingFuncList[val.func]) || val.func)
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.isWriteList[val.is_write])
            reStr += this.$t(',起始地址范围:') + val.addr_begin + '~' + val.addr_end
            break
          case 'commserver':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.isWriteList[val.is_write])
            break
          case 'mitsubishi':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.mitsubishiFuncList[val.func]) || val.func)
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.isWriteList[val.is_write])
            reStr += this.$t(',起始地址范围:') + val.addr_begin + '~' + val.addr_end
            reStr += this.$t(',下限:') + val.min_value
            reStr += this.$t(',上限:') + val.max_value
            break
          case 'cdt':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',帧类型:') + this.$t(this.ruleArray.frameList[val.frame])
            reStr += this.$t(',目的地址:') + val.target
            reStr += this.$t(',点号最小值:') + val.min_point
            reStr += this.$t(',点号最大值:') + val.max_point
            break
          case 'dlt645':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',数据操作类型:') + this.$t(this.ruleArray.operationList[val.operation])
            break
          case 'iec101':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',配电实施版本:') + this.ruleArray.applyVersionList[val.apply_version]
            reStr += this.$t(',控制类型:') + this.$t(this.ruleArray.controlList[val.control])
            reStr += this.$t(',公共地址范围:') + val.common_start_address + '~' + val.common_end_address
            reStr += this.$t(',信息地址范围:') + val.info_start_address + '~' + val.info_end_address
            break
          case 'EGD':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',Exchange ID范围:') + val.exchange_id_begin + '~' + val.exchange_id_end
            break
          case 'cimplicity':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',使能:') + this.$t(this.ruleArray.enableList[val.enable])
            break
          case 'pnio_cm':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.ruleArray.pniocmFuncList[val.data_type] || val.data_type)
            reStr += this.$t(',模块属性:') + this.ruleArray.pniocmBlockTypeShowList[val.block_type]
            reStr += this.$t(',操作对象:') + this.ruleArray.pniocmIndexShowList[val.index]
            break
          case 'custom':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',规则名称:') + this.$t(this.rule_name)
            break
          case 's7_300_plus': // 协议名称:s7 300 plus,动作:允许,访问类型:字符访问,寄存器区间:1,读写属性:null,DB 值:1,起始地址范围:0.1~0.2;
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',访问类型:') + this.$t(this.ruleArray.accessModeList[val.access_mode])
            if (val.access_mode === 'absolute_access') {
              reStr += this.$t(',寄存器区间:') + this.$t(this.ruleArray.s7PlusIntervalList['0x' + '00'.substr(0, (2 - val.register_interval.toString(16).length)) + val.register_interval.toString(16).toLowerCase()])
              if (val.register_interval === 138) {
                reStr += this.$t(',DB值:') + val.db_num
              }
            }
            const func16 = '0x' + '0000'.substr(0, (4 - val.func.toString(16).length)) + val.func.toString(16).toLowerCase()
            reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.s7PlusFuncList[func16])
            if (val.access_mode === 'absolute_access') {
              let register_address_max_position = val['register_address_max'] & 7
              let register_address_min_position = val['register_address_min'] & 7
              let register_address_max = val['register_address_max'] >> 3
              let register_address_min = val['register_address_min'] >> 3
              reStr += this.$t(',寄存器地址范围:') + register_address_min + '.' + register_address_min_position + '-' + register_address_max + '.' + register_address_max_position
            }
            break
          case 'mysql':
            reStr += this.$t(',匹配日志:') + this.$t(this.ruleArray.actionMysqlList[val.action])
            reStr += this.$t(',查询:') + this.$t(this.ruleArray.selectList[val.select])
            reStr += this.$t(',删除:') + this.$t(this.ruleArray.selectList[val.delete])
            reStr += this.$t(',修改:') + this.$t(this.ruleArray.selectList[val.update])
            reStr += this.$t(',插入:') + this.$t(this.ruleArray.selectList[val.insert])
            break
          case 'MMS':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + (this.$t(this.ruleArray.funcMmsList[String(val.func)]) || val.func)
            switch (String(val.func)) {
              case '1':
                reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteTypeList[String(val.read_write_type)])
                reStr += this.$t(',匹配范围:') + this.$t(this.ruleArray.acceptRangeList[val.accept_range])
                reStr += this.$t(',域名:') + val.domain_name
                reStr += this.$t(',变量名:') + val.variable_name
                break
              case '2':
                reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteType2List[String(val.variable_operate)])
                break
              case '3':
                reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteType3List[String(val.file_operate)])
                break
              case '4':
                reStr += this.$t(',读写属性:') + this.$t(this.ruleArray.readWriteType4List[String(val.log_operate)])
                break
            }
            break
          case 'enip':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList2[val.action])
            reStr += this.$t(',功能码:') + val.enip_commond
            break
          case 'cip':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',点类型:') + this.$t(this.ruleArray.tagTypeCipList[val.TagType])
            for (let index in val.CommDesTypeName) {
              let valueList = val.CommDesTypeName[index].array_info.split('-')
              reStr += (index === '0' ? ',' : '、') + this.$t('功能码') + valueList[0]
              reStr += valueList[1] === 'null' ? '' : (this.$t(',描述') + valueList[1])
              reStr += this.$t(',类型') + valueList[2]
              reStr += valueList[3] === 'any' ? '' : (this.$t(',点名') + valueList[3])
            }
            break
          case 'cipmb':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList2[val.action])
            reStr += this.$t(',功能码:') + (this.ruleArray.commondList[val.commond] || val.commond)
            reStr += this.$t(',匹配范围:') + this.$t(this.ruleArray.acceptRangeList[val.accept_range])
            if (val.start_addr) {
              reStr += this.$t(',起始地址:') + val.start_addr
            }
            if (val.end_addr) {
              reStr += this.$t(',结束地址:') + val.end_addr
            }
            break
          case 'mqtt':
            reStr += this.$t(',动作:') + this.$t(this.ruleArray.actionList[val.action])
            reStr += this.$t(',功能码:') + this.$t(this.ruleArray.mqttFUNCList[val.msg_type])
            break
          default:
            break
        }
        reStr += str
      })
      return reStr
    }
  }
}
</script>
